home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / kbhit.c < prev    next >
Text File  |  1995-08-13  |  2KB  |  60 lines

  1. /*
  2. kbhit.c
  3. kbhit() returns 1 if a keypress awaits processing, 0 otherwise.
  4. getcharUnbuffered() always returns immediately. It returns value -1 if there's 
  5. no character to return.
  6.  
  7. getcharUnbuffered() is used by Choose.c
  8.  
  9. 6/13/90    dgp wrote kbhit, based on suggestion from Michael Kahl and an earlier version by
  10.         Evan Relkin.
  11. 12/25/93 dgp cosmetic editing
  12. 8/1/94 dgp added support for MetroWerks CodeWarrior C 3.5.
  13. 10/1/94 dgp updated for MetroWerks CodeWarrior C 4.5, which still doesn't support
  14.         unbuffered input from the console.
  15. 10/8/94    dgp Discovered that GetNextEvent() works fine for unbuffered input.
  16. 1/7/95    dgp Updated to take advantage of the CW5 SIOUX console's new ability to handle an event.
  17. 6/18/95 dgp changed abort() to exit(1) for better compatibility with CW atexit().
  18. */
  19. #include "VideoToolbox.h"
  20.  
  21. int kbhit(void)
  22. {
  23.     #if THINK_C
  24.         int c;
  25.     
  26.         c=getcharUnbuffered();
  27.         if(c==EOF)return 0;
  28.         ungetc(c,stdin);
  29.         return 1;
  30.     #else
  31.         EventRecord event;
  32.         
  33.         return EventAvail(keyDownMask,&event);
  34.     #endif
  35. }
  36.  
  37. int getcharUnbuffered(void)
  38. {
  39.     int c;
  40.  
  41.     #if THINK_C
  42.         csetmode(C_RAW,stdin);        /* unbuffered: no echo, no editing */
  43.         c=getchar();
  44.         csetmode(C_ECHO,stdin);        /* default mode: line-buffered, echo, full editing */
  45.     #else
  46.         EventRecord event;
  47.  
  48.         while(!GetNextEvent(keyDownMask,&event)){
  49.             #if __MWERKS__
  50.                 // Allow the console to respond to the mouse, e.g. drag, zoom, or resize.
  51.                 if(GetNextEvent(mDownMask|mUpMask,&event))SIOUXHandleOneEvent(&event);
  52.             #endif
  53.         }
  54.         c=event.message&charCodeMask;
  55.         if(c=='.' && (event.modifiers&cmdKey))exit(1);
  56.     #endif
  57.     return c;
  58. }
  59.  
  60.